﻿<%@ Page Title="" Language="C#" MasterPageFile="~/OneColumn.Master" AutoEventWireup="true"
    CodeBehind="UnfinishedProducts.aspx.cs" Inherits="KCL.Summary.UnfinishedProducts" EnableEventValidation="true"%>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageHeader" runat="server">
  <link type="text/css" href="../styles/themes/base/jquery.ui.all.css" rel="stylesheet" />
    <script type="text/javascript" src="../styles/ui/jquery.ui.core.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.widget.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.position.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.mouse.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.draggable.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.dialog.js"></script>
    <script type="text/javascript" src="../styles/ui/jquery.ui.button.js"></script>
    <script type="text/javascript" src="../styles/external/jquery.bgiframe-2.1.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#divProduct").dialog({
                autoOpen: false,
                modal: true,
                width: 400,
                height: 240,
                maxHeight: 700,
                title: 'Chuyển đổi thành phẩm',
                resizable: false
            });
            $("#btnSave").button();
        });

        function ApplyExchange() {

            if (isNaN($("#transferedFinishQuantity").val()) == true || $("#transferedFinishQuantity").val() == "") {
                alert("Xin nhập số ở dòng số thành phẩm muốn chuyển!");
                return;
            }   

            $.ajax({
                url: "UnfinishedProducts.ashx?",
                type: "POST",
                dataType: "script",
                data: {
                    transferedFinishQuantity: $("#transferedFinishQuantity").val(),
                    sourceProductOrderId: $('input[@name="sourceProductIdentifier"]:checked').val(),
                    destinationProductOrderId: $("#destinationProductOrderId").val(),
                    action: $("#action").val()
                }
            });
        }

        function DisplayProduct(id) {
            var isRadioChecked = $('input[@name="sourceProductIdentifier"]:checked').size();

            if (isRadioChecked == 0) {
                alert("Bạn chưa chọn đơn hàng để lấy thành phẩm!");
                return;
            }  

            var currentRow = $("#link" + id).parent().parent().children();            
            $("#currentFinishedQuantity").val($(currentRow[7]).html());          
            var sourceProductOrderId = $('input[@name="sourceProductIdentifier"]:checked').val(); 
            $("#destinationProductOrderId").val(id);                       
            $("#action").val("edit");
            $("#divProduct").dialog("open");
        }

        function UpdateResults() {
            var sourceProductOrderId = $('input[@name="sourceProductIdentifier"]:checked').val();
            var sourceProductRow = $("#link" + sourceProductOrderId).parent().parent().children();
            var sourceProductFinishedQuantity = $(sourceProductRow[7]).html();

            var transferedFinishQuantity = $("#transferedFinishQuantity").val();
           
            if(parseInt(sourceProductFinishedQuantity) < parseInt(transferedFinishQuantity))
            {
                alert("Số thành phẩm muốn chuyển vượt quá số thành phẩm hiện có của đơn hàng " + sourceProductOrderId + "!");
                return;
            }

            $("#newFinishedQuantity").val(parseInt($("#currentFinishedQuantity").val()) + parseInt($("#transferedFinishQuantity").val()));
        }
   
    </script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <table>
        <tr>
            <td colspan="2">
                <span style="font-size: 25px; color: #474747">TÌM KIẾM TỒN THÀNH PHẨM</span>
            </td>
        </tr>
        <tr>
            <td>
                Kích thước
            </td>
            <td>
                <asp:DropDownList ID="ddlSize" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                Nguyên liệu
            </td>
            <td>
                <asp:DropDownList ID="ddlMaterial" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                Quy cách
            </td>
            <td>
                <asp:TextBox ID="txtDescription" runat="server" Width="300"></asp:TextBox>
            </td>
            <td>
                <asp:Button ID="btnSearch" runat="server" Text="Tìm" Width="120" 
                    onclick="btnSearch_Click" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
        </tr>
    </table>
    <div>
        <asp:ObjectDataSource ID="dsUnfinishedProducts" TypeName="KCL.Code.Data.Search" SortParameterName="sortExpression"
            SelectMethod="GetUnfinishedProducts" SelectCountMethod="GetTotalUnfinishedProducts"
            EnablePaging="True" runat="server">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlSize" Name="size" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="ddlMaterial" Name="material" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="txtDescription" Name="description" 
                    PropertyName="Text" Type="String" />
                <asp:Parameter Name="maximumRows" Type="Int32" />
                <asp:Parameter Name="startRowIndex" Type="Int32" />
                <asp:Parameter Name="sortExpression" Type="String" />
            </SelectParameters>
        </asp:ObjectDataSource>
        <asp:GridView ID="gridUnfinishedProducts" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" BackColor="Wheat" BorderColor="Firebrick" BorderStyle="Ridge"
            BorderWidth="1px" CellPadding="7" DataSourceID="dsUnfinishedProducts" 
            GridLines="None" Width="100%" runat="server" EnableModelValidation="True">
            <HeaderStyle BackColor="Black" ForeColor="Azure" Font-Bold="true" Wrap="false" />
            <FooterStyle ForeColor="Maroon" BackColor="DarkGoldenrod" />
            <PagerStyle ForeColor="Snow" HorizontalAlign="Right" BackColor="SaddleBrown" />
            <RowStyle BackColor="Ivory" />
            <Columns>
                <asp:BoundField HeaderText="Mã đơn hàng" DataField="orderId" SortExpression="orderId"
                    Visible="false" />
                <asp:BoundField HeaderText="Số đơn hàng" DataField="code" SortExpression="code" />
                <asp:BoundField HeaderText="Mã khách hàng" DataField="customerId" SortExpression="customerId" />
                <asp:BoundField HeaderText="Tên khách hàng" DataField="name" SortExpression="name" />
                <asp:BoundField HeaderText="Mã nguyên liệu" DataField="materialId" SortExpression="materialId" />
                <asp:BoundField HeaderText="Quy cách" DataField="description" SortExpression="description" />
                <asp:BoundField HeaderText="Kích thước" DataField="size" SortExpression="size" />
                <asp:BoundField HeaderText="Số lượng" DataField="orderQuantity" SortExpression="orderQuantity" />
                <asp:BoundField HeaderText="Hoàn thành" DataField="finishedQuantity" SortExpression="finishedQuantity" />
                <asp:TemplateField HeaderText="Chuyển thành phẩm">
                    <ItemTemplate>
                        <a href="javascript:DisplayProduct(<%# Eval("orderId")%>)" id="link<%# Eval("orderId")%>">                        
                            <img src="../styles/images/bodycontent/edit.jpg" alt="Chuyển thành phẩm" border="0" /></a>
                            <input type="radio" id="sourceProduct" name="sourceProductIdentifier" value="<%# Eval("orderId")%>" style="margin-top:13px;"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <AlternatingRowStyle BackColor="SeaShell" />
        </asp:GridView>
        <i>Đang xem trang
            <%=gridUnfinishedProducts.PageIndex + 1%>
            /
            <%=gridUnfinishedProducts.PageCount %></i>
    </div>
    <table>
        <tr>
            <td align="center" style="width: 100%">
                <center>
                    <asp:Button ID="btnBack" runat="server" Text="Trở về trang trước" OnClick="btnBack_Click" /></center>
            </td>
        </tr>
    </table>
     <div id="divProduct">
        <table border="0" cellpadding="7px" cellspacing="3px">
            <tr>
                <td>
                    Số thành phẩm hiện tại
                </td>
                <td>
                    <input type="text" id="currentFinishedQuantity" disabled="disabled" style="color: #ebdd00; text-align: center; font-weight:bold;"/>
                </td>
            </tr>
            <tr>
                <td>
                    Số thành phẩm muốn lấy
                </td>
                <td>
                    <input type="text" id="transferedFinishQuantity" onchange="UpdateResults()" style="color: #ebdd00; text-align: center; font-weight:bold;"/>
                </td>
            </tr>
            <tr>
                <td>
                    Số thành phẩm cuối cùng
                </td>
                <td>
                    <input type="text" id="newFinishedQuantity" disabled="disabled" style="color: #ea3737; text-align: center; font-weight:bold;"/>
                </td>
            </tr>                     
        </table>
        <p />
        <center>
            <input type="button" id="btnSave" value="&nbsp;Lưu&nbsp;" onclick="ApplyExchange()" /></center>
        <input type="hidden" value="create" id="action" />
        <input type="hidden" value="-1" id="destinationProductOrderId" />
    </div>
</asp:Content>
